package userInterface;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;

public class LeftPalleteView extends JPanel
{
	public LeftPalleteView()
	{
		this.setName("leftView");
		
		// set variables
		this.setBackground(new Color(64, 85, 93));
		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		this.setPreferredSize(new Dimension(70, 500));
		this.setFocusable(false);
		
		_btnFileNames = new HashMap<String, String>();
		_btnSelectFileNames = new HashMap<String, String>();
		
		// adding my buttons
		addButtons();
	}
		
	private void addButtons()
	{
		createButton("select", "src/select.gif", "src/selectSelect.gif");
		createButton("line", "src/line.gif", "src/lineSelect.gif");
		createButton("rectangleFull", "src/rectangleFull.gif", "src/rectangleFullSelect.gif");
		createButton("rectangleEmpty", "src/rectangleEmpty.gif", "src/rectangleEmptySelect.gif");
		createButton("ellipseFull", "src/ellipseFull.gif", "src/ellipseFullSelect.gif");
		createButton("ellipseEmpty", "src/ellipseEmpty.gif", "src/ellipseEmptySelect.gif");
		createButton("curve", "src/curve.gif", "src/curveSelect.gif");
		createButton("text", "src/text.gif", "src/textSelect.gif");
	}
	
	private JButton createButton(String name, String filename, String clickFilename)
	{
		_btnFileNames.put(name, filename);
		_btnSelectFileNames.put(name, clickFilename);

		JButton btn = new JButton(new ImageIcon(filename));
		
		btn.setName(name);

		btn.setBorderPainted(false);
		btn.setContentAreaFilled(false);
		btn.setFocusable(false);

		this.add(btn);
		
		return btn;
	}
	

	
	@Override
	public synchronized void addMouseListener(MouseListener l)
	{
		super.addMouseListener(l);
		
		for (Component c : this.getComponents())
			c.addMouseListener(l);
	}

	public void unselectButton()
	{
		if (_selectedButton != null)
		{
			_selectedButton.setIcon(new ImageIcon(_btnFileNames.get(_selectedButton.getName())));
			_selectedButton = null;
			super.repaint();
		}
	}
	
	public void setSelectedButton(JButton btn)
	{
		unselectButton();

		btn.setIcon(new ImageIcon(_btnSelectFileNames.get(btn.getName())));
		_selectedButton = btn;
		super.repaint();
	}
	
	private JButton _selectedButton;
	private Map<String, String> _btnSelectFileNames;
	private Map<String, String> _btnFileNames;
}
